/**
 * 
 */
package com.ksv.lop;

import java.util.ArrayList;

import com.ksv.qlsv.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SinhVienAdapter extends BaseAdapter {

	private Context mContext;
	public ArrayList<SinhVien> mArrSinhVien;

	public SinhVienAdapter(Context context, Cursor cur) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mArrSinhVien = getDataFromCursor(cur);
	}
	
	private ArrayList<SinhVien> getDataFromCursor(Cursor c){
		ArrayList<SinhVien> mang = new ArrayList<SinhVien>();
		c.moveToFirst();
		SinhVien sv = null;
		while (c.getPosition() >= 0) {
			sv = new SinhVien();
			sv.setMaSV(c.getInt(c.getColumnIndex(ConnectDB.masv)));
			sv.setHoSV(c.getString(c.getColumnIndex(ConnectDB.hosv)));
			sv.setTenSV(c.getString(c.getColumnIndex(ConnectDB.tensv)));
			sv.setGioiTinh(Boolean.valueOf(c.getString(c.getColumnIndex(ConnectDB.gioitinh))));
			sv.setDiaChi(c.getString(c.getColumnIndex(ConnectDB.diachi)));
			sv.setMaLop(c.getInt(c.getColumnIndex(ConnectDB.malop)));
			mang.add(sv);
			if(c.getPosition() >= c.getCount() - 1)
				break;
			c.moveToNext();
//			Log.i("TTTH",sv.getTenSV().toString()+" - "+ sv.getMaSV());
		}
		return mang;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrSinhVien.size();
	}

	@Override
	public SinhVien getItem(int position) {
		// TODO Auto-generated method stub
		return mArrSinhVien.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return mArrSinhVien.get(position).getMaSV();
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		LayoutInflater layoutInflater = LayoutInflater.from(mContext);
		LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.listrow, null);
		TableRow tblrow = (TableRow) linearLayout.findViewById(R.id.tableRow1);
		TextView txt = (TextView) tblrow.findViewById(R.id.txt_name);
		txt.setText(mArrSinhVien.get(position).getHoTen());
		return linearLayout;
	}
	
	public void setCheckedAll(){
		for (int i = 0; i < mArrSinhVien.size(); i++) {
			mArrSinhVien.get(i).setChecked(true);
		}
		this.notifyDataSetChanged();
	}
	
	public void setUnCheckedAll(){
		for (int i = 0; i < mArrSinhVien.size(); i++) {
			mArrSinhVien.get(i).setChecked(false);
		}
		this.notifyDataSetChanged();
	}
	
	public void Add(SinhVien sv){
		mArrSinhVien.add(sv);
		this.notifyDataSetChanged();
	}
	
	public void Clear(){
		mArrSinhVien.clear();
		this.notifyDataSetChanged();
	}
	
	public void Remove(int index){
		mArrSinhVien.remove(index);
		this.notifyDataSetChanged();
	}
	
	public void Update(SinhVien sv, int index){
		mArrSinhVien.get(index).setHoSV(sv.getHoSV());
		mArrSinhVien.get(index).setTenSV(sv.getTenSV());
		mArrSinhVien.get(index).setGioiTinh(sv.getGioiTinh());
		mArrSinhVien.get(index).setDiaChi(sv.getDiaChi());
		mArrSinhVien.get(index).setMaLop(sv.getMaLop());
		this.notifyDataSetChanged();
	}

}
